<?php
namespace app\data_magic\widget;

/**
 * 用来创建选项卡部件时需要的数据
 */
class TabsWidget extends Widget
{
    /**
     * @var array $tab_actions 操作列表
     */
    public $tab_actions=[];

    /**
     * 设置关联的操作，并且将其分类
     * @param array $actions 操作列表
     */
    public function setActions($actions)
    {
        foreach($actions as $action){
            if($action["data_type"]==="nope"){
                $this->tab_actions[]=$action;
            }
            elseif($action["data_type"]==="where" && $action["action_type"]==="search"){
                $this->tab_actions[]=$action;
            }
        }
        return $this;
    }

    /**
     * 设置当前操作
     */
    public function setCurrentAction($id=0,$name="")
    {
        foreach($this->tab_actions as $index => $action){
            $action["is_active"] = false;
            if($id!==0 && isset($action["id"])){
                $action["is_active"] = intval($id) === intval($action["id"]);
            }
            if($name!==""){
                $action["is_active"] = $name === $action["name"];
            }
            $this->tab_actions[$index]=$action;
        }
        return $this;
    }

    public function toArray()
    {
        return [
            "tab_actions"=>$this->tab_actions,
        ];
    }
}
